/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
package com.wealth.bbs.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import iaas.enums.StatusEnum;
import iaas.persistence.DynamicSpecifications;
import iaas.persistence.SearchFilter;
import iaas.persistence.SearchFilter.Operator;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.ResultObject;

import com.wealth.bbs.entity.Reply;
import com.wealth.bbs.entity.Topic;
import com.wealth.bbs.repository.ReplyDao;
import com.wealth.bbs.repository.TopicDao;
import com.wealth.bbs.service.IReplyService;
import com.wealth.bbs.vo.ReplyVO;
import com.wealth.member.entity.Member;
import com.wealth.member.repository.MemberDao;

/**
 * @author zhu
 * @version 1.0
 * @since 1.0
 */
@Component
public class ReplyServiceImpl extends BaseServiceImpl<Reply> implements IReplyService{

		@Autowired
		private ReplyDao replyDao;
		@Autowired
		private TopicDao topicDao;
		@Autowired
		private MemberDao memberDao;

		@Override
		public List<ReplyVO> ajaxReplyVO(Long topicid, Long memberid, Integer index, Integer offset) throws Exception {
			
			/** ---- 分页查询 ---*/
			//创建分页请求.
			Sort sort = new Sort(Direction.ASC,"id");	//正序排列（类似贴吧，用户关注抢沙发）
			PageRequest pageRequest = new PageRequest(0, offset, sort);
			//创建动态查询条件组合.
			Collection<SearchFilter> filters = new ArrayList<SearchFilter>();
			//过滤:板块
			filters.add(new SearchFilter("topic.id", Operator.EQ, topicid));
			//过滤:状态
			filters.add(new SearchFilter("status", Operator.EQ, StatusEnum.NORMAL.getValue()));
			//过滤：id<index
			if(index!=-1){
				filters.add(new SearchFilter("id", Operator.GT, index));
			}
			Specification<Reply> spec = DynamicSpecifications.bySearchFilter(filters, Reply.class);
			Page<Reply> page  = replyDao.findAll(spec, pageRequest);
			Topic topic = topicDao.findOne(topicid);
			/** ---- 组装vo ---*/
			List<Reply> replyList = page.getContent();
			List<ReplyVO> replyvolist = new ArrayList<ReplyVO>();
			for (Reply reply : replyList) {
				ReplyVO rvo = new ReplyVO();
				rvo.setContent(reply.getContent());
				rvo.setId(reply.getId());
				rvo.setCreateTime(reply.getCreateTime());
				rvo.setNickname(reply.getMember().getNickname());
				rvo.setHeadimgurl(reply.getMember().getHeadimgurl());
				if(reply!=null){
					//我评论的帖子 || 我发的帖子 可以删除
					if(reply.getMember().getId().equals(memberid)  || topic.getMember().getId().equals(memberid)){
						rvo.setIsDelete("1");
					}
				}
				replyvolist.add(rvo);
			}
			return replyvolist;
		}

		@Override
		public ResultObject saveReply(Reply reply) throws Exception {
			reply.setCreateTime(new Date());
			reply.setStatus(StatusEnum.NORMAL.getValue());
			//reply.setIsRead(0);//设为未读
			replyDao.save(reply);
			
			ReplyVO vo = getReplyVo(reply.getId());
			return  new ResultObject("000", "回复成功", vo);
		}

		
		
		@Override
		public ReplyVO getReplyVo(Long replyid) throws Exception {
			ReplyVO rvo = new ReplyVO();
			Reply reply = replyDao.findOne(replyid);
			Member member = memberDao.findOne(reply.getMember().getId());
			rvo.setContent(reply.getContent());
			rvo.setId(reply.getId());
			rvo.setCreateTime(reply.getCreateTime());
			rvo.setNickname(member.getNickname());
			rvo.setHeadimgurl(member.getHeadimgurl());
			rvo.setSex(reply.getMember().getSex());
			return rvo;
		}

		
}
